package com.cndw.kungfu.model.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cndw.kungfu.core.CacheDataBase;
import com.cndw.kungfu.domain.Friend;
import com.cndw.kungfu.domain.Goods;
import com.cndw.kungfu.domain.LogCost;
import com.cndw.kungfu.domain.LogGot;
import com.cndw.kungfu.domain.LogPlayer;
import com.cndw.kungfu.domain.Pet;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.event.GameShutdownHandler;
import com.cndw.kungfu.ext.SerializeProcesser;
import com.cndw.kungfu.model.disciple.DiscipleManager;
import com.cndw.kungfu.model.disciple.domain.Disciple;
import com.cndw.kungfu.model.goods.GoodsManager;
import com.cndw.kungfu.model.pet.PetManager;
import com.cndw.kungfu.model.player.FriendManager;
import com.cndw.kungfu.model.player.PlayerDataInfo;
import com.cndw.kungfu.model.player.PlayerManager;

/**
 * 对象同步接口集合
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-10-6 下午2:18:34
 */
@Service
public class SyncServer {
	@Autowired
	private PlayerManager playerManager;
	@Autowired
	private DiscipleManager discipleManager;
	@Autowired
	private SysManger sysManger;
	@Autowired
	private GoodsManager goodsManager;
	@Autowired
	private LogManager logManager;
	@Autowired
	private PetManager petManager;
	@Autowired
	private FriendManager friendManager;

	
	private static boolean needClac = false;

	public boolean syncDB(Player player) {
		return playerManager.saveToDb(player);
	}


	public boolean syncDB(Disciple disciple) {
		return discipleManager.syncDiscipleToDB(disciple);
		//log.debug("sync disciple " + disciple.getDiscipleId() + " " + disciple.getPlayerId());
	}

	public boolean syncDB(Goods goods) {
		return goodsManager.saveDB(goods);
	}

	public boolean syncDB(PlayerDataInfo playerDataInfo) {
		boolean suc = false;
		try {
			String cacheData = SerializeProcesser.writeObject(playerDataInfo);
			if (cacheData != null) {
				suc = playerManager.saveTempData(playerDataInfo, cacheData);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (needClac) {
			GameShutdownHandler.debugOnDown.incrementAndGet();
		}
		//log.debug("sync dataInfo:" + playerDataInfo.getPlayerId());
		return suc;
	}
	
	public boolean syncDB(Pet pet){
		return petManager.save(pet);
	}


	public boolean syncDB(LogCost logCost) {
		return logManager.saveLogCost(logCost);
	}


	public boolean syncDB(CacheDataBase cacheDataBase) {
		boolean suc = false;
		try {
			String cacheData = SerializeProcesser.writeObject(cacheDataBase);
			if (cacheData != null) {
				suc = sysManger.saveToDb(cacheDataBase, cacheData);				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//log.debug("async sync cachedata:"+cacheDataBase.getKey());
		return suc;
	}


	public boolean syncDB(List<LogPlayer> logPlayers) {
		try {
			logManager.saveLogPlayer(logPlayers);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}


	public static void setNeedClac(boolean needClac) {
		SyncServer.needClac = needClac;
	}
	
	public int selectLastBuyTime(long playerId,int goodsId){
		return logManager.selectLastBuyTime(playerId,goodsId);
	}


	public boolean syncDB(LogGot logGot) {
		return logManager.saveLogGot(logGot);
	}
	
	public boolean syncDB(Friend friend){
		return friendManager.updateFriend(friend);
	}



}
